<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;

class Article extends Model
{

    use HasDateTimeFormatter;

	protected $table = 'article';

    // 追加到模型数组表单的访问器
    protected $appends = ['content_limit', 'http_logo'];

    // 输出时间戳格式
    protected $casts = [
        'created_at' => 'timestamp',
        'updated_at' => 'timestamp',
    ];

    // 截取内容
    public function getContentLimitAttribute()
    {
        return Str::limit(strip_tags($this->content), 50);
    }

    // 图片地址拼接域名前缀
    public function getHttpLogoAttribute()
    {
        return $this->logo ? config('filesystems.disks.public.url') . $this->logo : null;
    }

	public function getList($where = [], $with = [], $select = ['*'])
    {
        return $this->newQuery()
            ->when($with, function ($query, $with) {
                return $query->with($with);
            })
            ->select($select)
            ->where($where)
            ->orderBy('sort')
            ->get();
    }

    // 返回当前页数据列和总数据量
    public function getPaginate($where = [], $select = ['*'], $page = 1, $perPage = 10)
    {

        $data = $this->newQuery()
            ->where($where)
            ->orderBy('sort')
            ->paginate($perPage, $select, 'page', $page);

        return ['list' => $data->items(), 'total' => $data->total()];
    }

}
